import type {
  ConfigParameters,
  ConnectionDisconnectedReason,
  ConnectionState,
  IArRTCRemoteUser,
} from "ar-rtc-sdk";

import type {
  RtmStatusCode,
  RtmMessage,
  ChannelAttributes,
  RtmTextMessage,
} from "ar-rtm-sdk";

/** 私有云配置 */
export interface AnyRTCLiveConfig {
  rtcConfig?: ConfigParameters;
  rtmConfig?: ConfigParameters;
}
/** RTC 封装类的事件回调 */
export type RTCServiceEvents = {
  // sdk 与服务连接状态
  CconnectionStateChange: (
    curState: ConnectionState,
    revState: ConnectionState,
    reason?: ConnectionDisconnectedReason
  ) => void;
  // 用户加入
  userJoin: (user: IArRTCRemoteUser) => void;
  // 用户离开
  userLeft: (user: IArRTCRemoteUser, reason: string) => void;
  // 用户发布音频/视频
  userPublished: (user: IArRTCRemoteUser, mediaType: "audio" | "video") => void;
  // 用户停止发布音频/视频
  userUnpublished: (
    user: IArRTCRemoteUser,
    mediaType: "audio" | "video"
  ) => void;

  // RTM token 即将过期
  RTCTokenWillExpired: () => void;
  // RTM token 过期
  RTCTokenDidExpired: () => void;
};

/**
 * RTC 轨道发布状态
 *
 * @export
 * @enum {number}
 */
export enum RTCPublishState {
  IDLE = 0,
  PUBLISHING = 1,
  PUBLISHED = 2,
}

/**
 * RTM 封装类的事件回调
 */
export type RTMServiceEvents = {
  // rtm 与服务连接状态
  ConnectionStateChange: (
    newState: RtmStatusCode.ConnectionState,
    reason: RtmStatusCode.ConnectionChangeReason
  ) => void;
  // 点对点消息回调
  MessageFromPeer: (message: RtmMessage, peerId: string) => void;

  // ----   RTM 频道   -----
  // 频道属性改变之后会触发该回调
  ChannelAttributesUpdated(attributes: ChannelAttributes): void;
  // 频道成员人数更新回调
  ChannelMemberCountUpdated(memberCount: number): void;
  // 频道消息回调
  ChannelMessage(
    /*channelId: string, */ message: RtmMessage,
    memberId: string
  ): void;
  // 收到用户加入频道的通知
  ChannelMemberJoined(/*channelId: string, */ memberId: string): void;
  // 收到用户加入频道的通知
  ChannelMemberLeft(/*channelId: string, */ memberId: string): void;

  // RTM token 即将过期
  RTMTokenWillExpired: () => void;
  // RTM token 过期
  RTMTokenDidExpired: () => void;
};

/**
 * anyLive 封装类的事件回调
 */
export type AnyRTCLiveEvents = {
  // RTC 连接状态
  RTCConnectionStateChange: (
    curState: ConnectionState,
    revState: ConnectionState,
    reason?: ConnectionDisconnectedReason
  ) => void;

  // RTC 用户加入
  UserEnterRoom: (user: IArRTCRemoteUser) => void;
  // RTC 用户离开
  UserLeaveRoom: (user: IArRTCRemoteUser) => void;
  // RTC 用户发布
  UserAVEnabled: (user: IArRTCRemoteUser, mediaType: "audio" | "video") => void;
  // RTC 用户取消发布
  UserAVDisabled: (
    user: IArRTCRemoteUser,
    mediaType: "audio" | "video"
  ) => void;

  // RTM 连接状态
  RTMConnectionStateChange: (
    newState: RtmStatusCode.ConnectionState,
    reason: RtmStatusCode.ConnectionChangeReason
  ) => void;
  ChannelMemberJoined: (memberId: string) => void;
  ChannelMemberLeft: (memberId: string) => void;
  // RTM 频道人员数量变更
  ChannelMemberCountUpdated: (count: number) => void;
  // RTM 属性变更
  ChannelAttributesUpdated: (attributes: ChannelAttributes) => void;
  // 点对点消息
  UserMessage: (message: RtmTextMessage, memberId: string) => void;
  // 频道消息
  ChannelMessage: (message: RtmTextMessage, memberId: string) => void;

  // token 即将过期
  TokenWillExpired:(type: "RTM" | "RTC") => void;
  // token 过期
  TokenDidExpired: (type: "RTM" | "RTC") => void;
};
